﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Entities.Enums;
using AutoPartsSearch.Web.ViewModels;
using AutoPartsSearch.Web.ViewModels.Dictionary;

namespace AutoPartsSearch.Web.Controllers
{
    public class DictionariesController : Controller
    {
        private IDictionaryRepository _repository;

        public DictionariesController(IDictionaryRepository repository)
        {
            this._repository = repository;
        }

        public JsonResult ByType(DictionaryType type)
        {
            var result = this._repository.GetByType(type).OrderBy(x => x.Name.Default);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ByDictionaryTypeParent(DictionaryType type)
        {
            var groups = (from dicts in this._repository.GetByDictionaryTypeParent(type).OrderBy(x => x.Name.Default)
                                   group dicts by dicts.ParentName.Default into groupedDicts
                                   select new GroupedDictionary
                                   {
                                       Group = groupedDicts.Key,
                                       Items = groupedDicts,
                                       SubGroups = GetSubGroupsByAdditionalParentName(groupedDicts)
                                   }).OrderBy(x => x.Items.Count());
            var result = new List<GroupedDictionary>();
            foreach (var groupedDictionary in groups)
            {
                if (groupedDictionary.SubGroups == null)
                {
                    result.Add(groupedDictionary);
                }
                else
                {
                    result.AddRange(groupedDictionary.SubGroups);
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GroupedByAdditionalParentName(DictionaryType type)
        {
            var result = (from dicts in this._repository.GetByType(type).OrderBy(x => x.Name.Default)
                          group dicts by dicts.AdditionalParentName into groupedDicts
                          select new GroupedDictionary
                                     {
                                         Group = groupedDicts.Key,
                                         Items = groupedDicts
                                     });
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ByParent(int parentId)
        {
            var result = this._repository.GetByParentId(parentId).OrderBy(x => x.Name.Default);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private IEnumerable<GroupedDictionary> GetSubGroupsByAdditionalParentName(IEnumerable<Dictionary> dictionaries)
        {
            if (dictionaries.Count() == 0 || string.IsNullOrEmpty(dictionaries.ElementAt(0).AdditionalParentName))
            {
                return null;
            }
            var subGroup = (from dicts in dictionaries
                    group dicts by dicts.AdditionalParentName
                    into groupedAdditional
                    select new GroupedDictionary
                               {
                                   Group = groupedAdditional.Key,
                                   Items = groupedAdditional
                               });
            return subGroup;
        }
    }
}
